package cn.chencq.dataStructure.queue;

public class MyQueue {

	private int front;

	private int rear;

	private Object[] arr;

	private int maxSize;

	private int elems;

	public MyQueue(int maxSize) {
		this.maxSize = maxSize;
		arr = new Object[maxSize];
		elems = 0;
		front = 0;
		rear = -1;
	}

	public boolean isEmpty() {
		return elems == 0;
	}

	public boolean isFull() {
		return elems == maxSize;
	}

	public int size() {
		return elems;
	}

	public void insert(Object obj) {
		if (rear == maxSize - 1) {
			rear = -1;
		}
		arr[++rear] = obj;
		elems++;
	}

	public Object remove() {
		Object t = arr[front++];
		if (front == maxSize) {
			front = 0;
		}
		elems--;
		return t;
	}

	public Object peekFront() {
		return arr[front];
	}

	public static void main(String[] args) {
		MyQueue mq = new MyQueue(5);
		System.out.println(mq.isEmpty());
		mq.insert(30);
		mq.insert(20);
		mq.insert(10);
		mq.insert(2);
		mq.insert(1);
		mq.insert(20);

		System.out.println(mq.size());

		while (!mq.isEmpty()) {
			Object tmp = mq.remove();
			System.out.println(tmp + " ");
		}

	}
}
